package com.ea.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ea.admin.mapper.AdminAccountMapper;
import com.ea.admin.mapper.AdminDeptPermissionMapper;
import com.ea.admin.mapper.PositionInfoMapper;
import com.ea.admin.pojo.Enum.RoleCode;
import com.ea.admin.pojo.dto.PositionInfoDto;
import com.ea.admin.pojo.po.PositionInfo;
import com.ea.admin.pojo.vo.position.GetPositionListVo;
import com.ea.admin.pojo.vo.position.PositionVo;
import com.ea.admin.service.CompanyPermissionService;
import com.ea.admin.service.ConfigService;
import com.ea.admin.service.PositionService;
import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {
    @Resource
    PositionInfoMapper positionInfoMapper;
    @Resource
    ConfigService configService;
    @Resource
    CompanyPermissionService companyPermissionService;
    @Resource
    AdminAccountMapper adminAccountMapper;
    @Resource
    AdminDeptPermissionMapper adminDeptPermissionMapper;

    @Override
    public GetPositionListVo getPositionList(List<Long> companyIdList, String positionName, Integer page, Integer pageSize, Long accountId) {
        // 获得所有的单位权限
        List<Long> allCompanyIdList = companyPermissionService.getCompanyIdList(accountId);

        // 筛选部门管理员获取上级单位列表
        Long adminRoleId = adminAccountMapper.selectById(accountId).getAdminRoleId();
        if(adminRoleId== RoleCode.DEPT_SUPER_ADMIN.getCode().longValue()||
                adminRoleId== RoleCode.DEPT_ADMIN.getCode().longValue()){
            allCompanyIdList=adminDeptPermissionMapper.getCompanyIdListByAccountId(accountId);
        }

        if (allCompanyIdList == null || allCompanyIdList.isEmpty()) {
            throw new BizException(ErrorCode.USER_COMPANY_PERMISSION_ERROR);
        }
        if (companyIdList == null || companyIdList.isEmpty()) {
            companyIdList = allCompanyIdList;
        } else if (!new HashSet<>(allCompanyIdList).containsAll(companyIdList)) {
            throw new BizException(ErrorCode.USER_COMPANY_PERMISSION_ERROR);
        } else {
            companyIdList = new ArrayList<>(new HashSet<>(companyIdList));
        }

        Page<PositionInfoDto> positionInfoPage = positionInfoMapper.selectPositionPage
                (new Page<>(page, pageSize), companyIdList, positionName);
        GetPositionListVo result = new GetPositionListVo();
        result.setItems(parsePositionDtoListToVo(positionInfoPage.getRecords()));
        result.setTotal((int) (positionInfoPage.getTotal()));
        result.setPageSize((int) (positionInfoPage.getSize()));
        result.setCurrent((int) (positionInfoPage.getCurrent()));
        return result;
    }

    private List<PositionVo> parsePositionDtoListToVo(List<PositionInfoDto> records) {
        List<PositionVo> voList = new ArrayList<>();
        records.forEach(positionInfo -> {
            PositionVo vo = new PositionVo();
            vo.setPositionId(positionInfo.getPositionId().toString());
            vo.setPositionName(positionInfo.getPositionName());
            vo.setCompanyId(positionInfo.getCompanyId().toString());
            vo.setCompanyName(positionInfo.getCompanyName());
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public Long addPosition(String positionName, Long companyId, Long accountId) {
        // 检查是否拥有此单位的 单位管理权限
        companyPermissionService.checkCompanyPermission(accountId, companyId);


        // 检查职位名称是否已经存在
        LambdaQueryWrapper<PositionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PositionInfo::getPositionName, positionName);
        wrapper.eq(PositionInfo::getCompanyId, companyId);
        if (positionInfoMapper.selectCount(wrapper) > 0) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_EXISTS_POSITION_NAME_ERROR);
        }

        PositionInfo positionInfo = new PositionInfo();
        positionInfo.setPositionName(positionName);
        positionInfo.setCompanyId(companyId);
        positionInfo.setIsDel(0);
        positionInfoMapper.insert(positionInfo);
        return positionInfo.getPositionId();
    }

    @Override
    public void editPosition(Long positionId, String positionName, Long companyId, Long accountId) {
        // 检查职位是否存在
        PositionInfo positionInfo = positionInfoMapper.selectById(positionId);
        if (positionInfo == null) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_POSITION_NOT_EXISTS);
        }
        // 检查是否拥有此单位的 单位管理权限
        companyPermissionService.checkCompanyPermission(accountId, companyId);

        // 检查是否拥有修改前单位的管理权限
        if (!positionInfo.getCompanyId().equals(companyId)) {
            companyPermissionService.checkCompanyPermission(accountId, positionInfo.getCompanyId());
        }

        // 检查职位名称是否已经存在
        LambdaQueryWrapper<PositionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PositionInfo::getPositionName, positionName);
        wrapper.eq(PositionInfo::getCompanyId, companyId);
        if (positionInfoMapper.selectCount(wrapper) > 0) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_EXISTS_POSITION_NAME_ERROR);
        }
        // 修改职位信息
        positionInfo.setPositionName(positionName);
        positionInfo.setCompanyId(companyId);
        positionInfoMapper.updateById(positionInfo);
    }

    @Override
    public void deletePosition(Long positionId, Long companyId, Long accountId) {
        // 检查职位是否存在
        PositionInfo positionInfo = positionInfoMapper.selectById(positionId);
        if (positionInfo == null) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_POSITION_NOT_EXISTS);
        }
        // 检查是否拥有此单位的 单位管理权限
        companyPermissionService.checkCompanyPermission(accountId, companyId);

        // 检查职位是否有关联的人员
        if (positionInfoMapper.checkIfPositionHasUser(positionId)) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_EXISTS_POSITION_HAS_USER_ERROR);
        }
        // 删除
        positionInfoMapper.deleteById(positionId);
    }

    @Override
    public List<Long> getPositionIdList(Long accountId) {
        return positionInfoMapper.getPositionIdListByAccountId(accountId);
    }
}
